package com.bree.erp.jsf;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;

import com.bree.erp.entity.ItemRequest;
import com.bree.erp.entity.Project;
import com.bree.erp.entity.PurchaseRequisition;
import com.bree.erp.entity.Status;
import com.bree.erp.jsf.httpsess.IUserSession;
import com.bree.erp.jsf.httpsess.SessionDataKey;
import com.bree.erp.jsf.httpsess.UserSessionData;
import com.bree.erp.jsf.view.ItemRequestLazyView;
import com.bree.erp.service.ItemRequestService;
import com.bree.erp.service.ProjectService;
import com.bree.erp.service.PurchaseRequisitionService;

@ManagedBean(name="purchaseRequisitionBean")
@RequestScoped
public class PurchaseRequisitionBean {
	
	private static Logger LOG = Logger.getLogger(PurchaseRequisitionBean.class);
	
	private static final String PR_EDIT_PAGE = "/pr/edit";
	
	@ManagedProperty(value="#{userSession}")
	private UserSessionData userSession;
	
	@ManagedProperty(value="#{dtItemRequestLazyView}")
	private ItemRequestLazyView itemRequestLazyView;

	@EJB
	private PurchaseRequisitionService prService;
	
	@EJB
	private ItemRequestService irService;

	@EJB
	private ProjectService projectService;
	
	private PurchaseRequisition purchaseRequisition;
	
	private Long projectId;
	private String description;
	
	@PostConstruct
	public void initPurchaseRequisition() {

		if(purchaseRequisition == null) {
			Long id = userSession.get(SessionDataKey.SELECTED_PURCHASE_REQUISITION_ID, Long.class);
			purchaseRequisition = prService.read(id);
		}
		
		projectId = purchaseRequisition.getProject().getId();
		description = purchaseRequisition.getDescription();
	}
	
	public PurchaseRequisition getPurchaseRequisition() {
		
		//projectId = purchaseRequisition.getProject().getId();
		//description = purchaseRequisition.getDescription();
		
		return purchaseRequisition;
	}
	
	public String save() {
		
		List<Long> forDeletion = itemRequestLazyView.getForDeletion();
		if(forDeletion != null && forDeletion.isEmpty() == false) {
			for(Long delId : forDeletion) {
				LOG.debug("deleting ItemRequest: " + delId);
				irService.delete(delId);
			}
			
			itemRequestLazyView.initForDeletion();
		}
		
		// 1st part: get key details
		PurchaseRequisition pr = purchaseRequisition;
		LOG.debug("Saving basic info: " + pr.getId() + "; " + projectId + "; " + description);
				
		// 2nd part: item requests
		List<ItemRequest> list = itemRequestLazyView.getItemRequests();
		
		
		LOG.debug("save: " + list.size());
		
		for(ItemRequest item : list) {

			if(item.getId() != null && item.getId() > 0) {
				LOG.debug("updating item request: " + item.getId());
				
				irService.update(item.getId(), item.getQuantity(), item.getRemarks());
			} else {
				LOG.debug("creating item request: #" + item.getItemNumber());
				
				Long id = userSession.get(SessionDataKey.SELECTED_PURCHASE_REQUISITION_ID, Long.class);
				
				irService.create(id, item.getPartUnit().getId(), item.getQuantity(), item.getRemarks());
			}
			
		}
		
		// 3rd part: save + audit...
		purchaseRequisition = prService.update(pr.getId(), 
				projectId, description, 
				userSession.getLoggedInUser().getId());
		
		
		return PR_EDIT_PAGE;
	}
	
	public String submitForApproval() {

		PurchaseRequisition pr = purchaseRequisition;
		purchaseRequisition = prService.submit(pr.getId());
		
		save();
		
		return PR_EDIT_PAGE;
	}
	
	public String reopen() {
		
		PurchaseRequisition pr = purchaseRequisition;
		purchaseRequisition = prService.reopen(pr.getId(), 
				userSession.getLoggedInUser().getId());
		
		return PR_EDIT_PAGE;
	}
	
	public String approve() {
		PurchaseRequisition pr = purchaseRequisition;
		purchaseRequisition = prService.approve(pr.getId(), 
				userSession.getLoggedInUser().getId());
		
		return PR_EDIT_PAGE;
	}

	public List<Project> getProjects() {
		
		List<Project> list = new ArrayList<Project>();
		list.addAll(projectService.list());
		
		return list;
		
	}
	
	public boolean isStatusOpen() {
		if(purchaseRequisition != null) {
			return Status.OPEN == purchaseRequisition.getStatus();
		}
		
		return false;
	}

	public boolean isStatusPending() {
		if(purchaseRequisition != null) {
			return Status.PENDING == purchaseRequisition.getStatus();
		}
		
		return false;
	}
	
	public boolean isStatusApproved() {
		if(purchaseRequisition != null) {
			return Status.APPROVED == purchaseRequisition.getStatus();
		}
		
		return false;
	}

	/**
	 * @return the userSession
	 */
	public UserSessionData getUserSession() {
		return userSession;
	}

	/**
	 * @param userSession the userSession to set
	 */
	public void setUserSession(UserSessionData userSession) {
		this.userSession = userSession;
	}

	/**
	 * @return the itemRequestLazyView
	 */
	public ItemRequestLazyView getItemRequestLazyView() {
		return itemRequestLazyView;
	}

	/**
	 * @param itemRequestLazyView the itemRequestLazyView to set
	 */
	public void setItemRequestLazyView(ItemRequestLazyView itemRequestLazyView) {
		this.itemRequestLazyView = itemRequestLazyView;
	}

	/**
	 * @return the projectId
	 */
	public Long getProjectId() {
		return projectId;
	}

	/**
	 * @param projectId the projectId to set
	 */
	public void setProjectId(Long projectId) {
		this.projectId = projectId;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	
}
